import 'package:boss/http/api/api_helper.dart';
import 'package:boss/http/api/url_apis.dart';

import 'package:boss/ui/page/reportForm/billSummary/provider/bill_summary_provider.dart';
import 'package:boss/ui/page/reportForm/util/dialog.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loading/loading.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/bill_summary_browser.dart';

@RouterConfig(BillSummaryPage.pageName)
class BillSummaryPage extends StatefulWidget {
  static const pageName = '/billSummaryPage';

  @override
  _BillSummaryPageState createState() => _BillSummaryPageState('账单汇总');
}

class _BillSummaryPageState extends BaseState<BillSummaryPage> {
  _BillSummaryPageState(String pageName) : super(pageName);

  //发送邮箱
  sendMailbox(String e, String startTime, String endTime) async {
    showLoadingToast();
    try {
      var res = await analyticSMS(
        url: Apis.sendEmailOrderpropInfo,
        startTime: startTime + ' 00:00:00',
        endTime: endTime + ' 23:59:59',
        email: e ?? '',
      );
      if (res['code'] == HttpRespCode.OK) {
        toast("邮箱发送成功!");
        Navigator.pop(context);
      } else {
        toast(res['msg']);
      }
    } catch (e) {} finally {
      hideLoadingToast();
    }
  }

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(
          create: (context) => BillSummaryProvider(),
        ),
      ],
      child: Scaffold(
        appBar: CenterTitleAppBar(
          title: '账单汇总',
          actions: <Widget>[
            Consumer<BillSummaryProvider>(builder: (context, billSummaryProvider, _) {
              return LabelButton(
                title: '发送邮箱',
                padding: EdgeInsets.only(left: 8, top: 2, bottom: 2),
                onTap: () {
                  showChangeEmailAlert(
                    context,
                    title: '请输入邮箱',
                    hintText: '请输入您的邮箱地址',
                    onConfirmText: '提交',
                    onConfirm: (emil) async {
                      sendMailbox(
                        emil,
                        billSummaryProvider.startDateTime.toString().substring(0, 10),
                        billSummaryProvider.endDateTime.toString().substring(0, 10),
                      );
                    },
                  );
                },
              );
            }),
          ],
        ),
        body: BillSummaryBrowser(),
      ),
    );
  }
}
